package com.c.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.c.entity.OrderSetting;
import com.c.service.OrderSettingService;
import com.c.utils.DateUtils;
import lombok.SneakyThrows;

import java.sql.Date;
import java.util.Objects;

/**
 * @author wangxiangli
 * @create 2022-07-22-14:03
 */
public class OrderSettingExcelListener extends AnalysisEventListener<OrderSettingData> {

    //因为SubjectExcelListener不能交给spring进行管理，需要自己new，不能注入其他对象
    //不能实现数据库操作
    public OrderSettingService orderSettingService;
    public OrderSettingExcelListener() {
    }

    public OrderSettingExcelListener(OrderSettingService orderSettingService) {
            this.orderSettingService=orderSettingService;
    }


    @SneakyThrows
    @Override
    public void invoke(OrderSettingData orderSettingData, AnalysisContext analysisContext) {
        //判断当前日期是否已经进行了预约设置
        QueryWrapper<OrderSetting> orderSettingQueryWrapper=new QueryWrapper<>();
        java.util.Date date = DateUtils.parseString2Date(orderSettingData.getOrderDate());
        orderSettingQueryWrapper.eq("orderDate", DateUtils.parseString2Date(orderSettingData.getOrderDate()));
        OrderSetting orderSetting = orderSettingService.getOne(orderSettingQueryWrapper);
        if(Objects.isNull(orderSetting)){
            OrderSetting orderSetting1=new OrderSetting();
            orderSetting1.setOrderDate(new java.sql.Date(date.getTime()));
            orderSetting1.setNumber(orderSettingData.getNumber());
            orderSettingService.save(orderSetting1);
        }else{
            orderSetting.setReservations(orderSettingData.getNumber());
            orderSettingService.updateById(orderSetting);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
